Using plain string event names in even listeners is an anti-pattern; if the event is renamed, the application can start behaving unexpectedly. A
constant variable should be used instead.
Noncompliant code example
import flash.display.Sprite;
import flash.events.MouseEvent;
class ChildSprite extends Sprite
{
public function ChildSprite()
{
...
addEventListener("CustomEvent", clickHandler); // Noncompliant
}
}
function clickHandler(event:CustomEvent):void
{
trace("clickHandler detected an event of type: " + event.type);
trace("the this keyword refers to: " + this);
}
Compliant solution
import flash.display.Sprite;
import flash.events.MouseEvent;
class ChildSprite extends Sprite
{
public const CUSTOM_EVENT:String = "CustomEvent";
public function ChildSprite()
{
...
addEventListener(CUSTOM_EVENT, clickHandler);
}
}
function clickHandler(event:CustomEvent):void
{
trace("clickHandler detected an event of type: " + event.type);
trace("the this keyword refers to: " + this);
}